package com.atguigu.gulimall.gulimallware.service.impl;

import com.atguigu.common.constant.PurchaseConstant;
import com.atguigu.gulimall.gulimallware.service.WareSkuService;

import com.atguigu.gulimall.gulimallware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.gulimallware.service.PurchaseDetailService;

import com.atguigu.gulimall.gulimallware.vo.MergeVo;
import com.atguigu.gulimall.gulimallware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.gulimallware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.gulimallware.dao.PurchaseDao;
import com.atguigu.gulimall.gulimallware.entity.PurchaseEntity;
import com.atguigu.gulimall.gulimallware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;



@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Autowired
    WareSkuService wareSkuService;
    @Autowired
    PurchaseDetailService detailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1));
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void mergePurchase(MergeVo vo) {
        Long purchaseId = vo.getPurchaseId();
        if (purchaseId==null){
        //新建采购单
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setStatus(PurchaseConstant.PurchaseStatusEnum.CREATED.getCode());
        purchaseEntity.setCreateTime(new Date());
        purchaseEntity.setUpdateTime(new Date());
        this.save(purchaseEntity);
        purchaseId=purchaseEntity.getId();
    }
        //合并采购单
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = vo.getItems().stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(PurchaseConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        detailService.updateBatchById(collect);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

    }

    @Override
    public void received(List<Long> ids) {
        //确认采购单是新建或者已分配状态
        List<PurchaseEntity> collect = ids.stream().map(item->{
            PurchaseEntity entity = this.getById(item);
            return entity;
        }).filter(item -> {
            if (item.getStatus() == PurchaseConstant.PurchaseStatusEnum.CREATED.getCode() || item.getStatus() == PurchaseConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            } else {
                return false;
            }
        }).map(i->{
            i.setStatus(PurchaseConstant.PurchaseStatusEnum.RECEIVE.getCode());
            i.setUpdateTime(new Date());
            return i;
        }).collect(Collectors.toList());
        //改变采购单的状态
        this.updateBatchById(collect);
        //改变采购项的状态
        collect.forEach(item->{
          List<PurchaseDetailEntity> entities = detailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = entities.stream().map(entity -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(entity.getId());
                purchaseDetailEntity.setStatus(PurchaseConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            detailService.updateBatchById(collect1);

        });

    }

    @Override
    @Transactional
    public void done(PurchaseDoneVo vo) {
        Long id = vo.getId();
        ArrayList<PurchaseDetailEntity> updates = new ArrayList<>();
        //改变采购项的状态
        Boolean flag = true;
        for (PurchaseItemDoneVo item : vo.getItems()) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (item.getStatus()==PurchaseConstant.PurchaseStatusEnum.HASERROR.getCode()){
                purchaseDetailEntity.setStatus(item.getStatus());
                flag = false;
            }else {
                purchaseDetailEntity.setStatus(PurchaseConstant.PurchaseStatusEnum.FINISH.getCode());
                //将成功采购的进行入库
                PurchaseDetailEntity entity = detailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
            }
            purchaseDetailEntity.setId(item.getItemId());
            updates.add(purchaseDetailEntity);
        }
        detailService.updateBatchById(updates);
        //改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?PurchaseConstant.PurchaseStatusEnum.FINISH.getCode():PurchaseConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

}